package redinftel.utilidades;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.Part;
import redinftel.entidades.Multimedia;

/**
 *
 * @author Six Degrees Team
 */
public class ManejadorMultipart extends HttpServletRequestWrapper {
    
  private Hashtable<String, String[]> params = new Hashtable<String, String[]>();
    public ManejadorMultipart(HttpServletRequest request) {
        super(request);
        System.out.println("Creando manejador de formulario multipart");
        try{
        for (Part p : request.getParts()) {
                byte[] b = new byte[(int) p.getSize()];
                p.getInputStream().read(b);
                params.put(p.getName(), new String[]{new String(b)});
            }
        } catch (IOException ex) {
            Logger.getLogger(ManejadorMultipart.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ServletException ex) {
            Logger.getLogger(ManejadorMultipart.class.getName()).log(Level.SEVERE, null, ex);
        }
     
    }
/**
 *
 * @param request
 * @return
 */
     public List<Multimedia> obtenerMultimedia(HttpServletRequest request)
    {
        List<Multimedia> lista = new ArrayList<Multimedia>();

        try {
            for(Part p :((HttpServletRequestWrapper)request).getParts())
            {
                byte[] b = new byte[(int) p.getSize()];
                if(p.getName().contains("file"))
                {
                    Multimedia m = new Multimedia();
                    m.setTipo(p.getContentType());                    
                    p.getInputStream().read(b);
                    m.setImagen(b);

                    //Obtener el nombre del fichero
                    for (String cd : p.getHeader("content-disposition").split(";")) {
                    if (cd.trim().startsWith("filename")) {
                        m.setNombre(cd.substring(cd.indexOf('=') + 1).trim().replace("\"", ""));
                    }
                    }
                m.setFecha(new Date());                
                lista.add(m);
                }else if(p.getName().contains("externo")){
                        Multimedia m= new Multimedia();
                        m.setTipo("video/youtube");
                        p.getInputStream().read(b);
                        m.setImagen(b);
                        m.setNombre("videoYoutube");
                        m.setFecha(new Date());
                        lista.add(m);
                }else{}
                
                
                params.put(p.getName(), new String[]{new String(b)});
            }
            return lista;
        } catch (Exception ex) {
            return null;//No se ha podido crear
        }
    }
    @Override
    public String getParameter(String name) {
      String [] values = getParameterValues(name);
      if(values == null || values.length == 0) {
        return null;
      }

      return values[0];
    }

    @Override
    public Map<String, String[]> getParameterMap() {
      return params;
    }

    @Override
    public Enumeration<String> getParameterNames() {
      return params.keys();
    }

    @Override
    public String[] getParameterValues(String name) {
      return params.get(name);
    }

}
